<?php

namespace app\Admin\controller;

use think\Controller;
use think\Request;
use think\Session;

class Login extends Controller
{
    public function login()
    {
        $data = \request()->except('/login');
        $result = $this->validate(
           $data,
            [
                'uname' => 'require',
                'password' => 'require',
                '__token__' => 'token'
            ]);
        if (true !== $result) {
           return  json(['code'=>300,'data'=>'','msg'=>$result]);
        }
        $arr = [];
        $arr['uname'] = input('uname');
        $arr['password'] =md5(md5(input('password')));
        $user = new \app\Admin\model\User();
        $info = $user->getOne($arr);
        if ($info){
            Session::set('uid',$info['uid']);
            return  json(['code'=>200,'data'=>'','msg'=>'登录成功']);
        }else{
            return  json(['code'=>300,'data'=>'','msg'=>'登录失败']);
        }
    }

    public function tologin()
    {
        return view('login');
    }

    public function logout(){
        Session::clear();
        $this->redirect('/to/login');
    }

}
